PC與松下FP系列PLC的通信
2005/7/11 17:19:00
摘要:介紹了VB和松下FP 系列PLC 之間的通信協(xié)議,并給予了實(shí)例說明. 關(guān)鍵詞:VB, FP. 一. 引言 PLC作為工業(yè)現(xiàn)場的控制器,以其性能穩(wěn)定,編程簡單,結(jié)構(gòu)緊湊,通用性強(qiáng)等優(yōu)點(diǎn),已得到了廣泛的運(yùn)用;而計(jì)算器在圖象處理,報(bào)表打印,中文顯示等方面具有很強(qiáng)的功能;在現(xiàn)代的控制系統(tǒng)中,已經(jīng)在很多地方將兩者結(jié)合起來使用,充分發(fā)揮兩者的優(yōu)勢,因此,PC和PLC之間的通信顯的尤為重要,大多數(shù)PLC的通信協(xié)議都采用的是MODBUS,而松下FP系列采用的是其專用的MEWTOCOL-COM協(xié)議.本文通過實(shí)例詳細(xì)講解了利用該協(xié)議,達(dá)到PC和PLC通信的目的. 二. 松下PLC的通信協(xié)議 松下FP系列PLC的通信協(xié)議是采用松下公司專用的MEWTOCOL-COM協(xié)議.在該協(xié)議中,數(shù)據(jù)傳送采用的是ASCII碼形式,首先由計(jì)算器發(fā)送指令,PLC接受到指令后,自動進(jìn)行相應(yīng)的響應(yīng),計(jì)算器根據(jù)PLC的響應(yīng),可以得知PLC是否正確執(zhí)行了剛剛發(fā)送的指令,這樣就構(gòu)成了一個交互式的操作,以確保PC 與 PLC之間的通信正常. MEWTOCOL-COM的指令格式如下: 指令信息: 應(yīng)答信息(正常時): 應(yīng)答信息(發(fā)生錯誤時): 其中, “ % ”為起始符號,標(biāo)記每一幀報(bào)文的開始. “ CR ”為結(jié)束符號,標(biāo)記每一幀報(bào)文的結(jié)束. “ AD ”為每一站PLC的地址,用兩位十六進(jìn)制數(shù)表示,如 01 則代表第一臺PLC. “ # ”, “ $ ”, “ ! ”標(biāo)記該幀報(bào)文為何種類型,其分別對應(yīng)為:指令信息,應(yīng)答信息(正常),應(yīng)答信息(錯誤). “ BCC ”為校驗(yàn)碼,為兩位十六進(jìn)制數(shù),其初值為“ 0 ”,然后從起始符開始與該幀報(bào)文中每一字節(jié)按位進(jìn)行異或運(yùn)算得到的. 三. PC端VB程序的編寫 PC端的程序我們采用VB6.0 和 MSCOMM32 控件來編寫.VB的通信控件MSCOMM32提供了簡單的串行口通信功能,使用它可以建立與串行口的連接,通過串行口連接到其它設(shè)備,發(fā)送命令,接受資料,以及監(jiān)視和響應(yīng)串行通信中發(fā)生的事件和錯誤等.我們只要通過上位機(jī)的程序,向PLC按照MEWTOCOL-COM協(xié)議規(guī)定的格式發(fā)送命令,PLC就可以作出相應(yīng)的響應(yīng).為了使讀者能夠清楚的了解兩者之間的通信,現(xiàn)舉例如下: 整個程序的界面如圖 (1) 所示. 程序的部分代碼如下: Private Sub Cmd_Connect_Click() Lbl_State.Caption = "正在建立連接,請稍后..." DoEvents MSComm1.PortOpen = True ‘建立與串口的連接. MSComm1.InputLen = 1 MSComm1.RThreshold = 1 ‘設(shè)置接受緩沖區(qū)接受到一個字符就產(chǎn)生OnComm事件. TempOutputStr = "%" & Trim(Combo2.Text) & "#RT" ‘Combo2為PLC的站別. CommandStr = "RT" ‘ “RT”命令用來讀取PLC當(dāng)前的相關(guān)信息. MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) ‘將傳送的內(nèi)容顯示在文本框中 Timer1.Enabled = True ‘ 系統(tǒng)開始計(jì)時,以判斷PC和PLC連接是否正常. End If End Sub Private Sub Cmd_Dt_Click() Dim I As Integer Dim TempWd As String If Option3.Value = True Then CommandStr = "RD" For I = 0 To 15 Text1(I).Text = "" Next I TempOutputStr = "%" & Trim(Combo2.Text) & "#RD" & "D" & Trim(UCase(Txt_Bof.Text)) & Trim(UCase(Txt_Eof.Text)) ‘” RD “’命令用來讀取DT中的數(shù)據(jù). MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) Else CommandStr = "WD" TempOutputStr = "%" & Trim(Combo2.Text) & "#WD" & "D" & Trim(UCase(Txt_Bof.Text)) & Trim(UCase(Txt_Eof.Text)) ‘” WD “命令用來將數(shù)據(jù)寫入到相應(yīng)的DT中. For I = 0 To Val(Txt_Eof.Text) - Val(Txt_Bof.Text) TempWd = Hex$(Val(Text1(I).Text)) Do While Len(TempWd) <> 4 TempWd = "0" & TempWd Loop TempWd = Right$(TempWd, 2) & Left$(TempWd, 2) TempOutputStr = TempOutputStr & TempWd Next I MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If End Sub Private Sub Cmd_Run_Click() ‘控制PLC運(yùn)行 CommandStr = "RM" RPBoolean = True MSComm1.Output = "%" & Trim(Combo2.Text) & "#RMR4A" & Chr$(13) Txt_PcPlc.Text = "%" & Trim(Combo2.Text) & "#RMR4A" End Sub Private Sub Cmd_Stop_Click() ‘控制PLC停止 CommandStr = "RM" RPBoolean = False MSComm1.Output = "%" & Trim(Combo2.Text) & "#RMP48" & Chr$(13) Txt_PcPlc.Text = "%" & Trim(Combo2.Text) & "#RMP48" End Sub Private Sub Cmd_TC_Click() Dim TempWs As String Dim TempWk As String If Option9.Value = True And Option11.Value = True Then CommandStr = "RS" ‘讀取定時器/計(jì)數(shù)器中的默認(rèn)值 TempOutputStr = "%" & Trim(Combo2.Text) & "#RS" & Trim(UCase(Txt_TC.Text)) & Trim(UCase(Txt_TC.Text)) MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option9.Value = True And Option12.Value = True Then CommandStr = "WS" ‘寫入定時器/計(jì)數(shù)器中的默認(rèn)值 TempOutputStr = "%" & Trim(Combo2.Text) & "#WS" & Trim(UCase(Txt_TC.Text)) & Trim(UCase(Txt_TC.Text)) TempWs = Hex$(Val(Text2.Text)) Do While Len(TempWs) <> 4 TempWs = "0" & TempWs Loop TempWs = Right$(TempWs, 2) & Left$(TempWs, 2) TempOutputStr = TempOutputStr & TempWs MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option10.Value = True And Option11.Value = True Then CommandStr = "RK" ‘讀取定時器/計(jì)數(shù)器中的經(jīng)過值 TempOutputStr = "%" & Trim(Combo2.Text) & "#RK" & Trim(UCase(Txt_TC.Text)) & Trim(UCase(Txt_TC.Text)) MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option10.Value = True And Option12.Value = True Then CommandStr = "WK" ‘寫入定時器/計(jì)數(shù)器中的經(jīng)過值 TempOutputStr = "%" & Trim(Combo2.Text) & "#WK" & Trim(UCase(Txt_TC.Text)) & Trim(UCase(Txt_TC.Text)) TempWk = Hex$(Val(Text2.Text)) Do While Len(TempWk) <> 4 TempWk = "0" & TempWk Loop TempWk = Right$(TempWk, 2) & Left$(TempWk, 2) MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If End Sub Private Sub Cmd_YR_Click() Dim TempYR As String Dim TempValue As String Dim BinValue As String Dim HexValue As String Dim I As Integer If Option1.Value = True Then TempYR = "Y" Else TempYR = "R" End If If Option5.Value = True And Option7.Value = True Then CommandStr = "RCS" ‘讀取單個觸點(diǎn)的狀態(tài) TempOutputStr = "%" & Trim(Combo2.Text) & "#RCS" & TempYR & Trim(UCase(Txt_YR.Text)) MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option6.Value = True And Option7.Value = True Then CommandStr = "WCS" ‘寫入單個觸點(diǎn)的狀態(tài) If Label22.BackColor = &HC0& Then TempValue = "1" Else TempValue = "0" End If TempOutputStr = "%" & Trim(Combo2.Text) & "#WCS" & TempYR & Trim(UCase(Txt_YR.Text)) & TempValue MSComm1.Output = TempOutputStr & CalBcc(TempOutputStr) & Chr$(13) Txt_PcPlc.Text = TempOutputStr & CalBcc(TempOutputStr) End If If Option5.Value = True And Option8.Value = True Then CommandStr = "RCC" ‘讀取字單元觸點(diǎn)的狀態(tài) TempOutputStr = "%" & Trim(Combo2.Text) & "#RCC" & TempYR & Trim(UCase(Txt_YR.Text)) & Trim(UCa
提交
查看更多評論